package cn.fl.mcr.controller;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrSideAgreementService;
import cn.fl.mcr.vo.McrSideAgreementVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.base.constant.BaseConstant;
import cn.lionbridgecapital.loan.model.condition.McrSideAgreementQC;
import cn.lionbridgecapital.loan.model.exception.BaseException;
import cn.lionbridgecapital.loan.model.exception.BaseExceptionConstant;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author YangXin
 * @Description: 小贷补签协议Controller
 * @date 2020/4/26 13:37
 */
@RestController
@RequestMapping(value = "/mcr/sideAgrContract")
public class McrSideAgrContractController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(McrSideAgrContractController.class);

    @Autowired
    private IMcrSideAgreementService mcrSideAgreementService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do")
    public ModelAndView logsList(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("mcr/mcrContract/mcrSideAgreementList");
        BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 初始化列表
     */
    @RequestMapping(value = "/initList.do")
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        final String methodName = "initList";
        // 初始化参数
        Pager page = new Pager();
        McrSideAgreementQC mcrSideAgreementQC = new McrSideAgreementQC();

        // 前端传输参数
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String cstId = request.getParameter("cstId");
        String contract = request.getParameter("contract");

        // 赋值查询条件
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        if (StringUtils.isNotEmpty(cstId)){
            mcrSideAgreementQC.setCstId(Long.parseLong(cstId));
        }
        mcrSideAgreementQC.setContNo(contract);
        page.setCondition(mcrSideAgreementQC);
        BaseLogUtils.info(logger, methodName, "补签协议.查询数据请求参数.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.mcrSideAgreementService.searchListPage(page);
        BaseLogUtils.info(logger, methodName, "补签协议.查询数据完成.Result:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());

        List<McrSideAgreementVO> list = (List<McrSideAgreementVO>) pager.getResultList();
        ListPageVO<McrSideAgreementVO> listPage = new ListPageVO<>(list, pager.getTotalCount());
        BaseLogUtils.info(logger, methodName, "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }

    @RequestMapping(value = "/signContract.do")
    public Object signContract(HttpServletRequest request, HttpServletResponse response) {
        final String methodName = "signContract";
        String userName = CurrentThreadContext.getCurrentUserName();

        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "签署失败");

        String cstId = request.getParameter("cstId");
        String contract = request.getParameter("contract");
        if (StringUtils.isEmail(cstId)){
            resultVO.setInfo("客户ID为空");
            return resultVO ;
        }
        if (StringUtils.isEmail(contract)){
            resultVO.setInfo("合同号为空");
            return resultVO ;
        }

        try {
            BaseLogUtils.info(logger, methodName, "签署补充协议.签署请求.cstId:"+cstId+",contract:" + contract,userName);
            DataResultDTO dataResultDTO = this.mcrSideAgreementService.signSideAgrContract(Long.valueOf(cstId), contract);
            BaseLogUtils.info(logger, methodName, "签署补充协议.返回结果.cstId:"+cstId+",result:" + JSONObject.toJSONString(dataResultDTO),userName);
            if (!Objects.isNull(dataResultDTO) && BaseConstant.IS_YESNO_YES == dataResultDTO.getSuccess()){
                resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                resultVO.setInfo("合同公司章签署成功");
            }
        } catch (BaseException ex) {
            BaseLogUtils.error(logger, methodName, "签署补充协议.异常.cstId:"+cstId+",ex:" + ex.getMessage(),ex);
            throw new BaseException(BaseExceptionConstant.COMMON_0002,ex.getMessage());
        } catch (Exception ex) {
            BaseLogUtils.error(logger, methodName, "签署补充协议.异常.cstId:"+cstId+",ex:" + ex.getMessage(),ex);
            throw new BaseException(BaseExceptionConstant.COMMON_0002,"签署补充协议异常");
        }
        BaseLogUtils.info(logger, methodName, "签署补充协议.返回参数.result:" + JSONObject.toJSONString(resultVO),userName);
        return resultVO;
    }

}
